package com.xinyue.auto.reader;

import org.dom4j.Element;

import com.xinyue.auto.reader.process.DynamicProcess;
import com.xinyue.auto.reader.process.HqlProcess;
import com.xinyue.auto.reader.process.IdProcess;
import com.xinyue.auto.reader.process.EntityInitProcess;
import com.xinyue.auto.reader.process.ManyProcess;
import com.xinyue.auto.reader.process.OneProcess;
import com.xinyue.auto.reader.process.OrderProcess;
import com.xinyue.auto.reader.process.PageProcess;
import com.xinyue.auto.reader.process.PropertyProcess;
import com.xinyue.auto.reader.process.QueryProcess;
import com.xinyue.auto.reader.process.Work;

/**
 * 单例工厂
 * 创建读取XML的processes的单例对象
 */
public class EntityFactory {
	//构造器
	private EntityFactory() {
	}
	
	//私有内部类
	private static class SingletonHolder{
        private static EntityFactory instance = new EntityFactory();
    }
	
	/**获得实例对象
	 * ProcessFactory单例
	 */
	public static EntityFactory getInstance() {
		return SingletonHolder.instance;
	}
	
	/**私有内部类
	 * 保证处理XML读取和验证对象始终是单例状态
	 */
	private static class SingletonProccess{
		private static Work work = new Work();
		//Xml数据
		private static DynamicProcess dyn = new DynamicProcess(work);
		private static HqlProcess hql = new HqlProcess(dyn);
		private static QueryProcess query = new QueryProcess(hql);
		private static PageProcess page = new PageProcess(query);
		private static OrderProcess order = new OrderProcess(page);
		private static ManyProcess many = new ManyProcess(order);
		private static OneProcess one = new OneProcess(many);
		private static PropertyProcess prop = new PropertyProcess(one);
		private static IdProcess id = new IdProcess(prop);
		
		//初始化参数，并做收尾
		private static EntityInitProcess init = new EntityInitProcess(id);
    }
	
	public Process initProccess(Element entity) throws Exception {
		Process.xml = entity;
		//注入值
		Inject.injectValue(SingletonProccess.init);
		
		Inject.injectValue(SingletonProccess.id);
		Inject.injectValue(SingletonProccess.prop);
		Inject.injectValue(SingletonProccess.one);
		Inject.injectValue(SingletonProccess.many);
		Inject.injectValue(SingletonProccess.order);
		Inject.injectValue(SingletonProccess.page);
		Inject.injectValue(SingletonProccess.query);
		Inject.injectValue(SingletonProccess.hql);
		Inject.injectValue(SingletonProccess.dyn);
		
		Inject.injectValue(SingletonProccess.work);
		
		return SingletonProccess.init;
	}
}
